package com.ruoyi.project.oms.paeaAuTypePointRel.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.project.oms.paeaAuTypePointRel.domain.PaeaAuTypePointRel;
import com.ruoyi.project.oms.paeaAuTypePointRel.mapper.PaeaAuTypePointRelMapper;
import com.ruoyi.project.oms.paeaAuTypePointRel.service.IPaeaAuTypePointRelService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 监控类型监控点关系Service业务层处理
 *
 * @author 关晓强
 * @date 2020-03-14
 */
@Service
@Slf4j
public class PaeaAuTypePointRelServiceImpl extends ServiceImpl<PaeaAuTypePointRelMapper, PaeaAuTypePointRel> implements IPaeaAuTypePointRelService
{

    @Autowired
    private PaeaAuTypePointRelMapper paeaAuTypePointRelMapper;

    /**
     * 查询监控类型监控点关系
     *
     * @param id 监控类型监控点关系ID
     * @return 监控类型监控点关系
     */
    @Override
    public PaeaAuTypePointRel selectPaeaAuTypePointRelById(String id)
    {
        return paeaAuTypePointRelMapper.selectPaeaAuTypePointRelById(id);
    }

    /**
     * 查询监控类型监控点关系列表
     *
     * @param paeaAuTypePointRel 监控类型监控点关系
     * @return 监控类型监控点关系
     */
    @Override
    public List<PaeaAuTypePointRel> selectPaeaAuTypePointRelList(PaeaAuTypePointRel paeaAuTypePointRel)
    {
        return paeaAuTypePointRelMapper.selectPaeaAuTypePointRelList(paeaAuTypePointRel);
    }

    /**
     * 新增监控类型监控点关系
     *
     * @param paeaAuTypePointRel 监控类型监控点关系
     * @return 结果
     */
    @Override
    public int insertPaeaAuTypePointRel(PaeaAuTypePointRel paeaAuTypePointRel)
    {
        return paeaAuTypePointRelMapper.insertPaeaAuTypePointRel(paeaAuTypePointRel);
    }

    /**
     * 修改监控类型监控点关系
     *
     * @param paeaAuTypePointRel 监控类型监控点关系
     * @return 结果
     */
    @Override
    public int updatePaeaAuTypePointRel(PaeaAuTypePointRel paeaAuTypePointRel)
    {
        return paeaAuTypePointRelMapper.updatePaeaAuTypePointRel(paeaAuTypePointRel);
    }

    /**
     * 删除监控类型监控点关系对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deletePaeaAuTypePointRelByIds(String ids)
    {
        return paeaAuTypePointRelMapper.deletePaeaAuTypePointRelByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除监控类型监控点关系信息
     *
     * @param id 监控类型监控点关系ID
     * @return 结果
     */
    @Override
    public int deletePaeaAuTypePointRelById(String id)
    {
        return paeaAuTypePointRelMapper.deletePaeaAuTypePointRelById(id);
    }

    /**
     * 监督模型分配监督点
     * @param auTypeId 监督模型Id
     * @param ids 分配的监督点Id
     * @return 受影响记录数
     */
    @Override
    public List<PaeaAuTypePointRel> auPointConfigSave(String auTypeId, String ids) {
        if (!StringUtils.isNull(auTypeId)) {
            this.remove(new UpdateWrapper<PaeaAuTypePointRel>().eq("au_type_id",auTypeId));
        }
        List<PaeaAuTypePointRel> paeaAuTypePointRelList = Lists.newArrayList();
        if (!StringUtils.isEmpty(ids)) {
            Arrays.stream(Convert.toStrArray(ids)).forEach(id -> {
                PaeaAuTypePointRel paeaAuTypePointRel = new PaeaAuTypePointRel();
                paeaAuTypePointRel.setAuTypeId(auTypeId);
                paeaAuTypePointRel.setAuPointId(id);
                paeaAuTypePointRelList.add(paeaAuTypePointRel);
            });
            this.saveBatch(paeaAuTypePointRelList);
            int addCount = paeaAuTypePointRelList.size();
            log.info(String.format("监督模型分配监督点成功：insert[%s]", addCount));
        }
        return paeaAuTypePointRelList;
    }
}
